<template>
    <div>
      <div>

        <div>
          <el-form :inline="true" :model="formSearch" class="demo-form-inline">
            <el-form-item label="班级">
              <el-cascader
                  placeholder="请选择班级"
                  v-model="formSearch.classid"
                  :options="classTree"
                  @change="cascaderChange"></el-cascader>
            </el-form-item>
            <el-form-item label="教学周">
              <el-select v-model="formSearch.week" placeholder="请选择">
                <el-option
                    v-for="(item,index) in weekOptions"
                    :key="index"
                    :label="item.label"
                    :value="item.value">
                </el-option>
              </el-select>
            </el-form-item>
            <el-form-item label="学期">
              <el-input type="number" v-model="formSearch.term_year" placeholder="请输入学期"></el-input>
            </el-form-item>
            <el-form-item>
              <el-button type="primary" @click="searchClick">查询</el-button>
            </el-form-item>
          </el-form>
        </div>
      </div>
      <div>
        <ScheduleTable :courses="scList" @editCourse="editCourseEvent"></ScheduleTable>
      </div>
      <!--      添加-->
      <el-dialog title="编辑班级"   :visible.sync="addFormVisible">
        <el-form :rules="rules" :model="addForm" label-position="right" ref="addForm" label-width="80px">
          <el-form-item label="课程名" prop="courseid">
            <el-select v-model="addForm.courseid" placeholder="请选择课程">
              <el-option
                  v-for="item in courseList"
                  :key="item.id"
                  :label="item.coursename"
                  :value="item.id">
              </el-option>
            </el-select>
          </el-form-item>
          <el-form-item label="开始周" prop="start_week">
            <el-input type="number"  v-model="addForm.start_week" autocomplete="off"></el-input>
          </el-form-item>
          <el-form-item label="结束周" prop="end_week">
            <el-input type="number" v-model="addForm.end_week" autocomplete="off"></el-input>
          </el-form-item>
          <el-form-item label="上课安排">
            <div>
              <el-checkbox-group v-model="addForm.pitch_num" size="small">
                <el-checkbox-button v-for="city in cityOptions" :label="city" :key="city">{{city}}节</el-checkbox-button>
              </el-checkbox-group>
            </div>
          </el-form-item>
          <el-form-item label="教室">
            <el-input v-model="classroom" autocomplete="off"></el-input>
          </el-form-item>
        </el-form>
        <div slot="footer" class="dialog-footer">
          <el-button @click="addFormVisible = false">取 消</el-button>
          <el-button type="primary" @click="addUserEvent">确 定</el-button>
        </div>
      </el-dialog>
    </div>
</template>

<script>
import ScheduleTable from "@/components/ScheduleTable";
import {getClass,addSc, getCourseAll, getScweek} from "@/request";
import {groupBykey} from "@/tools/utils";
export default {
  name: "AddSc",
  data(){
   return {
     addFormVisible:false,
     classroom:null,
     weekday:[],
     classTree:[],
     addForm:{
       courseid:null,
       term_year:null,
       start_week:null,
       end_week:null,
       week_day:[],
       pitch_num:[],
       class_room:[],
     },
     checkboxList:[],
     cityOptions:['1','2', '3', '4', '5', '6', '7'],
     courseList:[],
     rules: {
       courseid: [
         {required: true, message: '请输入课程名', trigger: 'blur'  },
       ],
       term_year: [
         {required: true, message: '请输入学期号', trigger: 'blur'  },
         { min: 1, message: '长度在 2 到 16 个字符', trigger: 'blur' }
       ],
       start_week: [
         {required: true, message: '请输入开始周', trigger: 'blur'  },
       ],
       end_week: [
         {required: true, message: '请输入结束周', trigger: 'blur'  },
       ]
     },
     scList:[],
     weekOptions: [],
     formSearch:{
       classid:null,
       week:1,
       term_year:"1"
     },
   }
  },
  components:{
    ScheduleTable
  },
  created() {
    this.getcourse();
    this.getscweek();
    this.getclassTree();
    this.getweekOptions();
  },
  methods:{
    getweekOptions(){
      let arr=[]
       for(let i=1;i<21;i++){
         arr.push({label:'第'+i+'周',value:i})
       }
      this.weekOptions=arr;
      // this.formSearch.week=1
    },
    cascaderChange(val){
      this.formSearch.classid=val&&val[1]
    },
    searchClick(){
      this.getscweek()
    },
    async getscweek(){
      let data=await getScweek(this.formSearch);
      if(data.code!=200)return this.$message.error(data.message);
      this.scList=data.data;
    },
    async getclassTree(){
      let data=await getClass();
      if(data.code!=200)return this.$message.error(data.message);
      let arr=groupBykey(data.data,'grade');
      this.classTree=arr;
      this.formSearch.classid=data.data&&data.data[0].classid
    },
    editCourseEvent(val,val2,info){
      // this.addFormVisible=true;
      this.weekday=[val+1,val2+1];
      // console.log(info);
      this.$router.push("/klasa/sc");
    },
    async getcourse(){
      let data=await getCourseAll();
      if(data.code!=200)return this.$message.error(data.message);
      this.courseList=data.data;
    },
    addUserEvent(){
      // let that=this;
      // this.$refs['addForm'].validate(async (valid)=>{
      //   if(!valid){
      //     return false;
      //   }
      //
      //   // classid
      //   let len=that.addForm['pitch_num'].length;
      //   that.addForm['period_num']=that.addForm['end_week']-that.addForm['start_week'];
      //   that.addForm['week_day']=Array(len).fill( this.weekday[1])
      //   that.addForm['class_room']=Array(len).fill(this.classroom)
      //   that.addForm['classid']=this.formSearch.classid;
      //   that.addForm['term_year']=this.formSearch.term_year;
      //
      //   console.log(that.addForm)
        // let data=await addSc(that.addForm);
        // if(data.code!=200)return this.$message.error(data.message);
        // this.$message.success('添加成功!');
        // this.addFormVisible=false;
        // this.getclass();
        // this.addForm={
        //   classid:null,
        //   classname:null,
        //   hover:null,
        //   grade:null,
        // }
      // })
    },
  }
}
</script>

<style scoped>

</style>